package com.zhongxi.guoyuan.view.shopCart;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.base.BaseActivity;
import com.zhongxi.guoyuan.fragment.cartstore.GoStoreFragment;
import com.zhongxi.guoyuan.fragment.cartstore.PostStoreFragment;
import com.zhongxi.guoyuan.fragment.cartstore.SameCityFragment;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zhongxi.guoyuan.R.id.btn_post_shop;
import static com.zhongxi.guoyuan.R.id.btn_self_shop;

/**
 * Created by Administrator on 2017/10/26.
 */

public class ShoppingCartActivity extends BaseActivity {

    @Bind(R.id.ivBack)
    ImageView ivBack;
    @Bind(R.id.tvTitle)
    TextView tvTitle;
    @Bind(R.id.ivDel)
    ImageView ivDel;
    @Bind(R.id.rl_container)
    RelativeLayout rlContainer;
    @Bind(R.id.title)
    RelativeLayout title;
    @Bind(R.id.btn_post_shop)
    Button btnPostShop;
    @Bind(R.id.btn_self_shop)
    Button btnSelfShop;
    @Bind(R.id.ll_carte)
    LinearLayout llCarte;
    @Bind(R.id.btn_same_city)
    Button mBtnSameCity;
    private Button[] buttons;
    private PostStoreFragment postStoreFragment;
    private GoStoreFragment goStoreFragment;
    private SameCityFragment mSameCityFragment;
    private Fragment[] fragments;
    private int index, currentIndex;

    Handler handler = new Handler() {
        private String mSameFlag;
        private String goFlag;
        private String postFlag;

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    postFlag = (String) msg.obj;
                    if (!TextUtils.isEmpty(postFlag)) {
                        index = 1;
                    }
                    break;
                case 2:
                    goFlag = (String) msg.obj;
                    if (!TextUtils.isEmpty(goFlag)) {
                        index = 0;
                    }
                    break;
                case 3:
                    mSameFlag = (String) msg.obj;
                    break;
            }
         /* if ((!TextUtils.isEmpty(postFlag) && !TextUtils.isEmpty(goFlag) && !TextUtils.isEmpty( )) || (TextUtils.isEmpty(postFlag))) {
                index = 0;
            } else if (!TextUtils.isEmpty(postFlag) && TextUtils.isEmpty(goFlag)) {
                index = 1;
            } else {
                index = 2;
            } */
            refresh();
        }
    };

    @Override
    protected void initView() {
        super.initView();
        setContentView(R.layout.activity_shopping_cart);
        ButterKnife.bind(this);
        tvTitle.setText("购物车");
    }

    @Override
    protected void initData() {
        super.initData();
        buttons = new Button[3];
        buttons[0] = btnPostShop;
        buttons[1] = btnSelfShop;
        buttons[2] = mBtnSameCity;
        buttons[0].setSelected(true);
        postStoreFragment = new PostStoreFragment(handler);
        goStoreFragment = new GoStoreFragment(handler);
        mSameCityFragment = new SameCityFragment(handler);
        fragments = new Fragment[]{postStoreFragment, goStoreFragment, mSameCityFragment};
        getSupportFragmentManager().beginTransaction().add(R.id.rl_container, postStoreFragment).hide(goStoreFragment).commit();
    }

    @OnClick({R.id.ivBack, R.id.ivDel, btn_post_shop, btn_self_shop, R.id.btn_same_city})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ivBack:
                finish();
                break;
            case R.id.ivDel:
                if (index == 0) {
                    postStoreFragment.setDetailDatas();
                } else if (index == 1) {
                    goStoreFragment.setDetailDatas();
                } else if (index == 2) {
                    mSameCityFragment.setDetailDatas();
                }
                break;
            case R.id.btn_post_shop:
                index = 0;
                break;
            case R.id.btn_self_shop:
                index = 1;
                break;
            case R.id.btn_same_city:
                index = 2;
                break;
        }
        refresh();
    }

    private void refresh() {
        if (index != currentIndex) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(fragments[currentIndex]);
            if (!fragments[index].isAdded()) {
                transaction.add(R.id.rl_container, fragments[index]);
            }
            transaction.show(fragments[index]).commit();
            buttons[index].setSelected(true);
            buttons[currentIndex].setSelected(false);
            currentIndex = index;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
